package com.blueocean_health.care.management.service.impl;

import java.util.List;
import java.util.UUID;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.base.UserToken;
import com.blueocean_health.care.management.domain.dto.ManagerDto;
import com.blueocean_health.care.management.domain.dto.PullDownDto;
import com.blueocean_health.care.management.domain.dto.UserDto;
import com.blueocean_health.care.management.mapper.UserMapper;
import com.blueocean_health.care.management.mapper.UserTokenMapper;
import com.blueocean_health.care.management.service.UserService;

/**
 * 用户模块 业务实现类  2017年8月22日16:42:43
 * @author wuyanli
 *
 */
@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserMapper userDao;
	
	@Autowired
	private UserTokenMapper tokenDao;
	
	@Value("${service.address}")
	private String serviceAddress;

	/**
	 * 根据医院Id 获取该医院下的所有负责人  2017年8月22日16:43:17
	 */
	@Override
	public List<ManagerDto> getManagerByHospital(String hospitalId) {
		List<ManagerDto> userList = userDao.getManagerByHospital(hospitalId);
		return userList;
	}

	/**
	 * 根据用户Id查询用户信息  2017年8月29日10:36:01
	 */
	@Override
	public UserDto getCurrentUserInfo(User user) {
		String password = user.getPassword();
		password = DigestUtils.md5Hex(password);
		user.setPassword(password);
		User currentUserInfo = userDao.getCurrentUserInfo(user);
		if(currentUserInfo == null){
			return null;
		}
		Integer userId = currentUserInfo.getUserId();
		UserToken userTokenInfo = getUserTokenInfo(userId+"");
		UserDto userDto = new UserDto();
		userDto.setUserId(userId);
		userDto.setHospitalName(currentUserInfo.getHospitalName());
		userDto.setUserName(currentUserInfo.getUserName());
		userDto.setUserRealName(currentUserInfo.getUserRealName());
//		System.out.println("serviceAddress:"+serviceAddress);
		userDto.setAvatar(serviceAddress+"defaultUser.jpg");
		userDto.setHospitalLogo(serviceAddress+"hospitalLogo.png");
		userDto.setToken(userTokenInfo.getToken());
//		currentUserInfo
		return userDto;
	}
	
	/**
	 * 保存token信息 2017年8月29日14:31:18
	 * @param userId
	 * @return
	 */
	private UserToken getUserTokenInfo(String userId){
		UserToken userToken = new UserToken();
		userToken.setToken(UUID.randomUUID().toString());
		userToken.setUserId(userId);
		int insertToken = tokenDao.insertToken(userToken);
		if(insertToken <= 0){
			return null;
		}
		return userToken;
	}
	@Override
	public User getUserInfoByToken(String token){
		UserToken userToken = tokenDao.getTokenByToken(token);
		if(userToken == null){
			return null;
		}
		String userId = userToken.getUserId();
		User user = new User();
		user.setUserId(Integer.parseInt(userId));
		User currentUserInfo = userDao.getUserByUserId(userId);
		return currentUserInfo;
	}

	@Override
	public List<PullDownDto> getFieldWorkerByHospitalId(String hospitalId) {
		List<PullDownDto> fieldWorker = userDao.getFieldWorkerByHospitalId(hospitalId);
		return fieldWorker;
	}
	@Override
	public List<User> getPushUserByHospitalId(String hospitalId) {
		List<User> pushUser = userDao.getPushUserByHospitalId(hospitalId);
		return pushUser;
	}
	public static void main(String[] args) {
		Double maxValue = Double.MAX_VALUE;
		System.out.println(maxValue.doubleValue());
		System.out.println(Double.MIN_VALUE);
	}

}
